package com.higgins.eventtracking.client.health.fundamentals;

import java.util.ArrayList;
import java.util.List;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericStatsTab;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class Fundamentals extends VerticalPanel	implements GenericTabInterface, GenericStatsTabParent	{
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();
	private TabPanel tabPanel = new TabPanel();
	private GenericStatsTab digestionTab = new GenericStatsTab(this, "Digestion", 225);
	private final GenericStatsTab hygieneTab = new GenericStatsTab(this, "Hygiene", 225);
	private WellBeingTab wellBeingTab = new WellBeingTab(this);
	private FrameOfMindTab frameOfMindTab = new FrameOfMindTab(this);
	private NegativeTab negativeTab = new NegativeTab(this);
	private AchievementTab achievementsTab = new AchievementTab(this);
	private boolean firstRun = true;
	private GenericStatsTabParent parent = null;
	private Integer tabIndex = null;
	private String tabTitle = null;
	private GenericChartPanel chartPanel = null;
	
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public String getTabTitle()	{
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}

	public Fundamentals(GenericStatsTabParent parent)	{
		this.parent = parent;
		int tabIndex = 0;
		digestionTab.setTabIndex(tabIndex++);
		digestionTab.setTabTitle("Digestion");
		hygieneTab.setTabIndex(tabIndex++);
		hygieneTab.setTabTitle("Hygiene");
		wellBeingTab.setTabIndex(tabIndex++);
		wellBeingTab.setTabTitle("Well Being");
		frameOfMindTab.setTabIndex(tabIndex++);
		frameOfMindTab.setTabTitle("Frame of Mind");
		negativeTab.setTabIndex(tabIndex++);
		negativeTab.setTabTitle("Negative");
		achievementsTab.setTabIndex(tabIndex++);
		achievementsTab.setTabTitle("Achievements");
	}

	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		addTabs();
		populateChartPanel();
		firstRun = false;
	}
	public void populateChartPanel()	{
		if (chartPanel!=null)	{
			add(chartPanel);
		}
	}

	public void populateTabPanel()	{
		digestionTab.update();
		hygieneTab.update();
		wellBeingTab.update();
		frameOfMindTab.update();
		achievementsTab.update();
		negativeTab.update();
		add(tabPanel);
	}
	private void addTabs()	{
		tabPanel.add(digestionTab, "Digestion", true);
		listOfTabs.add(digestionTab);

		tabPanel.add(hygieneTab, "Hygiene", true);				
		listOfTabs.add(hygieneTab);

		tabPanel.add(wellBeingTab, "Well Being", true);
		listOfTabs.add(wellBeingTab);

		tabPanel.add(frameOfMindTab, "FrameOfMind", true);
		listOfTabs.add(frameOfMindTab);

		tabPanel.add(negativeTab, "Emotional");
		listOfTabs.add(negativeTab);

		tabPanel.add(achievementsTab, "Achievements");
		listOfTabs.add(achievementsTab);

	
		tabPanel.selectTab(0);
		tabPanel.addSelectionHandler(createSelectionHandler());		
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
	        }
		};
		return selectionHandler;
	}
	@Override
	public void updateTabTitle(Integer index, String title, Boolean alert) {
		TabBar tabBar = tabPanel.getTabBar();
		tabBar.setTabHTML(index, alert?"<div class=\"highlightAlertLabel\" style=\"width: "+ (title.length()*9) +"px;\">" + title + "</div>":title);
		if (alert)	{
			parent.updateTabTitle(this.tabIndex, tabTitle, true);
		}
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		this.chartPanel = chartPanel;
	}

}
